// pages/information-intention/index.js
var util = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    // getCompanys: "index/getCompanys",
    // getJobs: "index/getJobs",
    // getCities: "index/getCities",
    // getDomainList: "index/getDomainList",
    // checkPhone: "user/checkPhone",
    // getByUnionid: "user/getByUnionid",   //获取用户基本信息
    // userSave: "user/save",
    visible:false,
    jobList:[],
    companyList:[],
    domainList:[],  //领域
    contactList:[
      {
        id:'0',
        name:'现在'
      },
      {
        id:'1',
        name:'一月后'
      },
      {
        id:'2',
        name:'半年后'
      },
      {
        id:'3',
        name:'一年后',
      }
    ],
    domain01:'',
    domain02:'',
    domain03:'',
    domainId01:'',
    domainId02:'',
    domainId03:'',
    company01:'',
    company02:'',
    company03:'',
    job:'',
    concactTime:'',
    detail:{
      majorDomain1: "",
      majorDomain2: "",
      majorDomain3: "",
      majorDomainId1: 0,
      majorDomainId2: 0,
      majorDomainId3: 0,
      expectCompanyName1:'',
      expectCompanyName2:'',
      expectCompanyName3:'',
      expectJobName:"",
      intentTime:"",
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 当编辑个人资料inut框获取默认数据
    util.post(app.api.getByUnionid,{unionid:app.globalData.unionid},(res)=>{
      if(res.name){
        this.setData({
          detail : res
        })
      }
      // 设置期望页如何联系我显示内容
      if(res.intentTime==0){
        this.setData({
          ['detail.intentTime'] : '现在'
        })
      }else if(res.intentTime==1){
        this.setData({
          ['detail.intentTime'] : '一月后'
        })
      }else if(res.intentTime==2){
        this.setData({
          ['detail.intentTime'] : '半年后'
        })
      }else if(res.intentTime==3){
        this.setData({
          ['detail.intentTime'] : '一年后'
        })
      }
    })
    //
    var that = this
    let promiseCount = 0
    let domain = 0
    let info = wx.getStorageSync('info')
    if (info) {
      if(info.trade==="医药"){
        domain = 2
      }else{
        domain = 1
      }
    }
    //获取职位列表
    util.post(app.api.getJobs,{i:domain},(res)=>{
      that.setData({
        jobList:res.data,
      },()=>{
        promiseCount++
        if(promiseCount == 3){
          that.setData({
            visible:true,
          })
        }
      })
    })
    //获取领域列表
    util.post(app.api.getDomainList,{i:domain},(res)=>{
      that.setData({
        domainList:res.data,
      },()=>{
        promiseCount++
        if(promiseCount == 3){
          that.setData({
            visible:true,
          })
        }
      })
    })
    //获取公司列表
    util.post(app.api.getCompanys,{i:domain},(res)=>{
      that.setData({
        companyList:res.data,
      },()=>{
        promiseCount++
        if(promiseCount == 3){
          that.setData({
            visible:true,
          })
        }
      })
    })
  },
  saveDomain01: function(e){
    console.log(e.detail.name,e.detail.id)
    this.setData({
      domain01 : e.detail.name,
      domainId01 : e.detail.id
    })
  },
  saveDomain02: function(e){
    this.setData({
      domain02 : e.detail.name,
      domainId02 : e.detail.id
    })
  },
  saveDomain03: function(e){
    this.setData({
      domain03 : e.detail.name,
      domainId03 : e.detail.id
    })
  },
  saveCompany01: function(e){
    this.setData({
      company01 : e.detail.name
    })
  },
  saveCompany02: function(e){
    this.setData({
      company02 : e.detail.name
    })
  },
  saveCompany03: function(e){
      this.setData({
        company03 : e.detail.name
      })
  },
  saveJob: function(e){
    this.setData({
      job : e.detail.name
    })
  },
  saveIntentTime: function(e){
    let id
    if(e.detail.name == '现在'){
      id = 0
    }else if(e.detail.name == '一月后'){
      id = 1
    }else if(e.detail.name == '半年后'){
      id = 2
    }else{
      id = 3
    }
    this.setData({
      intentTime : id
    })
  },
  submit: function(e){
    let info2 = {}
    info2['domain01'] = this.data.domain01
    info2['domain02'] = this.data.domain02
    info2['domain03'] = this.data.domain03
    info2['company01'] = this.data.company01
    info2['company02'] = this.data.company02
    info2['company03'] = this.data.company03
    info2['job'] = this.data.job
    info2['intentTime'] = this.data.intentTime
    info2['comment'] = e.detail.value.salary
    let info1 = wx.getStorageSync('info1')
    let info = wx.getStorageSync('info')
    let obj = {
      domainName:info.trade,
      name: info1.name,
      mobile:info1.phone,
      phone:info1.phone,
      companyName: info1.company,
      roleFullName: info1.job,
      roleName: info1.showJob,
      cityName: info1.city,
      workage: info1.workTime,
      bestdomain: info1.area,
      comment:info1.salary,
      majorDomain1: info2.domain01,
      majorDomain2: info2.domain02,
      majorDomain3: info2.domain03,
      majorDomainId1:this.data.domainId01 ? this.data.domainId01 : 0,
      majorDomainId2:this.data.domainId02 ? this.data.domainId02 : 0,
      majorDomainId3:this.data.domainId03 ? this.data.domainId03 : 0,
      expectCompanyName1: info2.company01,
      expectCompanyName2: info2.company02,
      expectCompanyName3: info2.company03,
      expectJobName: info2.job,
      intentTime: info2.intentTime,
      openid:app.globalData.unionid
    }
    // 如果期望公司和现在公司一样
    if(info1.company==info2.company01||info1.company==info2.company02||info1.company==info2.company03){
      this.showModal("请选择与现任公司不一样的期望公司")
      return
    }
    // 如果期望公司重复
    if(info2.company01==info2.company02||info2.company01==info2.company03||info2.company02==info2.company03){
      if(!info2.company01&&!info2.company02||!info2.company01&&!info2.company03||!info2.company02&&!info2.company03){
      }else{
        this.showModal("请选择不同的期望公司")
        return
      }
    }
    // 如果细分领域重复
    if(info2.domain01==info2.domain02||info2.domain01==info2.domain03||info2.domain02==info2.domain03){
      if(info2.domain01&&info2.domain02||info2.domain01&&info2.domain03||info2.domain02&&info2.domain03){
        this.showModal("请选择不同的期望领域")
        return
      }
    }
    this.validate(info2,obj) //参数1效验所用数据，参数2发送请求参数
  },
  // validate 效验表单数据
  validate: function(value,obj){
    let goNextPage = true
    console.log(value)
    let domain = value.domain01.concat(value.domain02).concat(value.domain03)
    let company = value.company01.concat(value.company02).concat(value.company03)
    let intentTime = 0
    if(value['intentTime']||value['intentTime']==0){
      intentTime = value['intentTime'].toString()
    }
    let newValue = {
      job:value['job'],
      intentTime:intentTime,
      domain,
      company,
    }
    for(let item in newValue){
      if(!newValue[item]){
        goNextPage = false
      }
    }
    if(goNextPage){
      this.submitDate(obj)
    }else{
      this.showModal()
    }
  },
  // validate效验不通过，报错
  showModal(msg) {
    console.log(msg)
    if(msg){
      wx.showModal({
        content: `${msg}`,
        showCancel: false,
      })
    }else{
      wx.showModal({
        content: '请输入必填内容',
        showCancel: false,
      })
    }
  },
  // 提交表单数据
  submitDate: function(obj){
    util.post(app.api.getByUnionid,{unionid:app.globalData.unionid},(res)=>{
      if(res.empid){
        wx.showModal({
          content: '修改信息成功',
          showCancel: false,
        })
        obj = Object.assign(obj,{personId:res.personId,empid:res.empid})
      }else{
        wx.showModal({
          content: '注册成功',
          showCancel: false,
        })
        obj = Object.assign(obj,{personId:0,empid:0})
      }
      setTimeout(()=>{
        util.post(app.api.userSave,obj,(res)=>{
          console.log(res,'提交信息')
          wx.switchTab({
            url: '/pages/index/index'
          })
        })
      },1000)
    })
  },
  //分享
  onShareAppMessage: function (res) {
    var that = this
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    var title = that.data.positionInfo.companyName + ' 招聘 ' + that.data.positionInfo.name
    return {
      // title: title,
      // path: 'pages/position-detail/position-detail?pid=' + that.data.positionInfo.id,
      // data: that.data.positionInfo,
      imageUrl: this.data.imgUrl,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },
  onShareTimeline: function (res) {
    var title = that.data.positionInfo.companyName + ' 招聘 ' + that.data.positionInfo.name
    return {
      title: title,
      query: 'pid=' + that.data.positionInfo.id,
    }
  },
})